

import java.util.Scanner;
public class test {
}

 class PascalTriangle {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入杨辉三角的行数：");
        int rows = scanner.nextInt();
        scanner.close();

        int[][] triangle = generatePascalTriangle(rows);
        printPascalTriangle(triangle);
    }

    public static int[][] generatePascalTriangle(int rows) {
        int[][] triangle = new int[rows][];
        for (int i = 0; i < rows; i++) {
            triangle[i] = new int[i + 1];
            for (int j = 0; j <= i; j++) {
                if (j == 0 || j == i) {
                    triangle[i][j] = 1;
                } else {
                    triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
                }
            }
        }
        return triangle;
    }

    public static void printPascalTriangle(int[][] triangle) {
        int rows = triangle.length;
        for (int i = 0; i < rows; i++) {
            // 打印前导空格，使杨辉三角居中显示
            for (int space = 0; space < rows - i - 1; space++) {
                System.out.print(" ");
            }
            // 打印当前行的数字
            for (int j = 0; j <= i; j++) {
                System.out.print(triangle[i][j] + " ");
            }
            System.out.println();
        }
    }
}
